package com.imooc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class SetTest extends ListTest {

	public List<Course> coursesToSelect;
	public SetTest() {
		this.coursesToSelect =  new ArrayList<Course>();
	}
	
	public void testAdd() {
		//创建一个课程对象
		Course cr1 = new Course("1", "数据结构");
		coursesToSelect.add(cr1);
		//Set集，无序，不重复
		Course cr2 = new Course("2", "Java程序设计");
		coursesToSelect.add(cr2);
		//可以把对象放到一个数组中，一起添加
		Course[] crs = {new Course("3", "算法精通"), new Course("4", "python从入门到放弃")};
		coursesToSelect.addAll(Arrays.asList(crs));
		
		Course[] crs2 = {new Course("5", "高等数学"), new Course("6", "计算机英语")};
		coursesToSelect.addAll(Arrays.asList(crs2));	
	}
	
	/**
	 * 通过for each 方法遍历
	 */
	public void testForEach() {
		System.out.println("有以下课程可选（for each遍历）：");
		for(Object obj : coursesToSelect) {
			Course cr = (Course)obj;
			System.out.println("课程：" + cr.id + "->"  + cr.name);
		}
	}
	
	public static void main(String[] args) {
		
		SetTest ts = new SetTest();
		ts.testAdd();
		ts.testForEach();
		
		//创建一个学生对象
		Student student = new Student("1", "大明");
		System.out.println("欢迎小学生：" + student.name + "选课！");
		//创建一个Scanner对象，接收选课id
		Scanner scan = new Scanner(System.in);
		
		for(int i = 0; i < 2; i++) {
			System.out.println("请输入课程id");
			String courseid = scan.next();
			for(Course cr : ts.coursesToSelect) {
				if(cr.id.equals(courseid)) {
					student.scourses.add(cr);
				}
			}
		}
		scan.close();
		
		ts.testForEachForSet(student);
	}
	
	public void testForEachForSet(Student student) {
		//输出学生选课
				for(Course cr : student.scourses) {
					System.out.println("选择了课程：" + cr.id + "->" + cr.name);
				}
	}
}
